package com.ctgu.tengyun.service.product.impl;

import com.ctgu.tengyun.bean.request.ProductRepairQuery;
import com.ctgu.tengyun.dao.mybatis.ProductRepairRecordMapper;
import com.ctgu.tengyun.domain.ProductRepairRecord;
import com.ctgu.tengyun.domain.ProductRepairRecordExample;
import com.ctgu.tengyun.service.product.ProductRepairRecordService;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/12.
 */
@Service
public class ProductRepairRecordServiceImpl implements ProductRepairRecordService {

    @Resource
    private ProductRepairRecordMapper mapper;

    @Override
    public int insertSelective(ProductRepairRecord record) {
        return mapper.insertSelective(record);
    }

    @Override
    public Integer countProductRepairRecord(ProductRepairQuery repairQuery) {
        ProductRepairRecordExample example = new ProductRepairRecordExample();
        ProductRepairRecordExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotEmpty(repairQuery.getProductNo())) {
            criteria.andProductNoEqualTo(repairQuery.getProductNo());
        }
        if (null != repairQuery.getRepairDate()) {
            LocalDate localDate = Instant.ofEpochMilli(repairQuery.getRepairDate()).atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDateTime todayBegin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime todayEnd = LocalDateTime.of(localDate, LocalTime.MAX);

            criteria.andRepairDateGreaterThanOrEqualTo(Date.from(todayBegin.atZone(ZoneId.systemDefault()).toInstant()));
            criteria.andRepairDateLessThanOrEqualTo(Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (null != repairQuery.getRepairType()) {
            criteria.andRepairTypeEqualTo(repairQuery.getRepairType());
        }
        if (null != repairQuery.getIsTrouble()) {
            criteria.andIsTroubleEqualTo(repairQuery.getIsTrouble());
        }
        criteria.andIsValidEqualTo(1);

        return mapper.countByExample(example);
    }

    @Override
    public List<ProductRepairRecord> selectProductRepairRecord(ProductRepairQuery repairQuery) {
        ProductRepairRecordExample example = new ProductRepairRecordExample();
        ProductRepairRecordExample.Criteria criteria = example.createCriteria();

        criteria.andIsValidEqualTo(1);
        if (StringUtils.isNotEmpty(repairQuery.getProductNo())) {
            criteria.andProductNoEqualTo(repairQuery.getProductNo());
        }
        if (null != repairQuery.getRepairDate()) {
            LocalDate localDate = Instant.ofEpochMilli(repairQuery.getRepairDate()).atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDateTime todayBegin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime todayEnd = LocalDateTime.of(localDate, LocalTime.MAX);

            criteria.andRepairDateGreaterThanOrEqualTo(Date.from(todayBegin.atZone(ZoneId.systemDefault()).toInstant()));
            criteria.andRepairDateLessThanOrEqualTo(Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (null != repairQuery.getRepairType()) {
            criteria.andRepairTypeEqualTo(repairQuery.getRepairType());
        }
        if (null != repairQuery.getIsTrouble()) {
            criteria.andIsTroubleEqualTo(repairQuery.getIsTrouble());
        }

        return mapper.selectByExample(example);
    }

    @Override
    public ProductRepairRecord selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ProductRepairRecord record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

}
